package mobilizator.app.add;

import mobilizator.app.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter implements
	OnClickListener {
    private Context context;
    private String[][] contents;
    private String[] titles;
    private boolean[][] checkedItems;

    /*
     * Length of all the tabs must be the same!!
     */
    public ExpandableListAdapter(Context context, String[] titles,
	    String[][] contents) {
	super();

	// ////////
	if (titles.length != contents.length)
	    throw new IllegalArgumentException(
		    "Titles and contents must be the same size!");

	this.context = context;
	this.titles = titles;
	this.contents = contents;

	checkedItems = new boolean[titles.length][];
	for (int i = 0; i < checkedItems.length; i++) {
	    checkedItems[i] = new boolean[contents[i].length];
	    for (int j = 0; j < checkedItems[i].length; j++) {
		checkedItems[i][j] = false;
	    }
	}

    }

    public String getChild(int groupPosition, int childPosition) {
	return contents[groupPosition][childPosition];
    }

    public long getChildId(int groupPosition, int childPosition) {
	return 0;
    }

    public View getChildView(int groupPosition, int childPosition,
	    boolean isLastChild, View convertView, ViewGroup parent) {

	ChildViewHolder holder;
	View rowView = (View) convertView;

	String position = contents[groupPosition][childPosition];

	LayoutInflater inflater = (LayoutInflater) context
		.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	rowView = inflater.inflate(R.layout.expandable_child_row, null, true);

	holder = new ChildViewHolder();
	holder.item = (TextView) rowView.findViewById(R.id.item_name);
	holder.checkbox = (CheckBox) rowView.findViewById(R.id.item_check_box);
	holder.groupPosition = groupPosition;
	holder.childPosition = childPosition;
	rowView.setTag(holder);
	rowView.setOnClickListener(this);

	holder.item.setText(position);
	holder.checkbox.setChecked(checkedItems[groupPosition][childPosition]);

	return rowView;
    }

    public int getChildrenCount(int groupPosition) {
	return contents[groupPosition].length;
    }

    public String[] getGroup(int groupPosition) {
	return contents[groupPosition];
    }

    public int getGroupCount() {
	return contents.length;
    }

    public long getGroupId(int groupPosition) {
	return 0;
    }

    public View getGroupView(int groupPosition, boolean isExpanded,
	    View convertView, ViewGroup parent) {
	GroupViewHolder holder;
	View rowView = (View) convertView;

	String title = titles[groupPosition];

	if (rowView == null) {
	    LayoutInflater inflater = (LayoutInflater) context
		    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    rowView = inflater.inflate(R.layout.expandable_group_row, null,
		    true);

	    holder = new GroupViewHolder();
	    holder.icon = (ImageView) rowView.findViewById(R.id.plus_or_minus);
	    holder.titleView = (TextView) rowView.findViewById(R.id.title);
	    rowView.setTag(holder);
	} else
	    holder = (GroupViewHolder) rowView.getTag();

	holder.titleView.setText(title);

	if (isExpanded)
	    holder.icon.setImageDrawable(context.getResources().getDrawable(
		    R.drawable.zwin));
	else
	    holder.icon.setImageDrawable(context.getResources().getDrawable(
		    R.drawable.rozwin));

	return rowView;
    }

    public boolean hasStableIds() {
	return false;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
	return true;
    }

    static class GroupViewHolder {
	public TextView titleView;
	public ImageView icon;

	public GroupViewHolder() {
	}
    }

    static class ChildViewHolder {
	public TextView item;
	public CheckBox checkbox;
	public int groupPosition, childPosition;

	public ChildViewHolder() {
	}
    }

    public void onClick(View v) {
	ChildViewHolder holder = (ChildViewHolder) v.getTag();
	holder.checkbox.toggle();
	togleCheckItem(holder.groupPosition, holder.childPosition);

    }

    private void togleCheckItem(int groupPosition, int childPosition) {
	checkedItems[groupPosition][childPosition] = !checkedItems[groupPosition][childPosition];
    }

    public boolean[][] getCheckedItems() {
	return checkedItems;
    }
}